package com.amos.cmp_android.ui;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.amos.cmp_android.R;
import com.facebook.drawee.backends.pipeline.Fresco;

/**
 * Created by tlh on 2016/4/13.
 */
public abstract class BaseActivity extends AppCompatActivity {
    protected Toolbar toolbar;
    protected RecyclerView recyclerView;
    protected SwipeRefreshLayout swipeRefresh;
    private int count;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Fresco.initialize(BaseActivity.this);
        setContentView(R.layout.activity_base);
    }

    protected abstract String getToolbarTitle();

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (count==0){
            initView();
            initData();
            count++;

        }
    }
    protected void initView(){
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        swipeRefresh = (SwipeRefreshLayout) findViewById(R.id.swipeRefresh);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        setSupportActionBar(toolbar);
        toolbar.setTitle(getToolbarTitle());
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
    protected void initData(){};

    @Override
    public void onBackPressed() {
        if (swipeRefresh.isRefreshing()){
            swipeRefresh.setRefreshing(false);
        }else {
            super.onBackPressed();
        }
    }
}
